<?php
namespace app\admin\controller;

use app\weixin\controller\Eswechat;
use app\common\model\Erp;
use think\Db;
use csv\Csv;

class Index extends Base
{
    public function csv()
    {
        header("Content-type:text/html;charset=utf-8");
        setlocale(LC_ALL, 'zh_CN');//linux系统下生效
        
        $csv = new Csv();  //实例化后才可以调用之前类文件定义好的方法
        // $list = \think\Db::table('d_accesses')->field('name, sign, pid, sort')->select();
        // $csv_title = array('名称','规则','PID','排序');
        // $csv->put_csv($list, $csv_title);
        $pathname = ROOT_PATH.'public'.DS.'uploads/exam2019-11-21 17_38_04.csv';
        $handle = fopen($pathname,'r');
        $list = $csv->input_csv($handle);

        dump($list);

    }

    //测试erp的同步服务功能
    public function erp()
    {
        //先获取，最大的用户注册时间
        $map['LASTTIME'] = strval(date('Ymd H:i:s', \think\Db::table('d_users')->max('modify_time')));
        // $map['LASTTIME'] = $max_time;

        $erp = new Erp();
        $list = $erp->erp_add($map);
        $list_arr = json_decode($list, 1);
        $list_toarray = $list_arr['item'];
        // dd($list_toarray);

        $insertData = [];
        foreach ($list_arr['item'] as $key => $value) {

            //组建数据
            $data = [
                'vip_number' => $value['VIPCARD'],
                'phone' => $value['MOBIL'],
                'truename' => $value['VIPNAME'],
                'level_id' => $value['VIPTYPE'],
                'gender' => $value['SEX']== 'M' ? '1' : '2',
                'user_end_time' => strtotime($value['VALIDDATE']),
                'birthday' => $value['BIRTHDAY'],
                'country' => $value['COUNTRY'],
                'city' => $value['CITY'],
                'create_at' => strtotime($value['CREATIONDATE']),
                'modify_time' => strtotime($value['MODIFIEDDATE']),
                'portrait' => $value['SEX']== 'M' ? 'default/gender1.png' : 'default/gender2.png',
                'status' => 1   
            ];

            //判断当前用户是否存在
            $is_user = Db::table('d_users')->where('phone', $value['MOBIL'])->find();

            if($is_user){
                //用户存在的话就修改用户的数据
                Db::startTrans();
                try{
                    Db::table('d_users')->where('phone', $value['MOBIL'])->update($data);
                    // 提交事务
                    Db::commit();    
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                }

            } else{
                //用户不存在的情况
                // 启动事务
                Db::startTrans();
                try{
                    Db::table('d_users')->insert($data);
                    // 提交事务
                    Db::commit();    
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                }

            }
            
        }

        // 启动事务
        Db::startTrans();
        try{
            Db::table('d_users')->insertAll($insertData);
            // 提交事务
            Db::commit();    
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }

    public function datatables()
    {

        // $list = \think\Db::table('d_ceshi')->select();
        $list = \think\Db::table('d_ceshi')->where('type', 1)->select();
        // $list = [];

        $menu = self::getMenuArr();

        return view('datatables', compact('menu', 'list'));
    }

    public function index()
    {
        // $wechat = new Eswechat();
        // $wechat->get_user_card_detail();
        
        //先获取，最大的用户注册时间
        // $max_time = strval(date('Ymd H:i:s', \think\Db::table('d_users')->max('create_at')));
        // dd($max_time);


        // $map['LASTTIME'] = $max_time;
        // // $map['LASTTIME'] = $max_time;

        // $erp = new Erp();
        // $list = $erp->erp_add($map);
        // $list_arr = json_decode($list, 1);
        // $list_toarray = $list_arr['item'];
        // // dd($list_toarray);

        // $insertData = [];
        // foreach ($list_arr['item'] as $key => $value) {

        //     $insertData[] = [
        //         'vip_number' => $value['VIPCARD'],
        //         'phone' => $value['MOBIL'],
        //         'truename' => $value['VIPNAME'],
        //         'level_id' => $value['VIPTYPE'],
        //         'gender' => $value['SEX']== 'M' ? '1' : '2',
        //         'user_end_time' => strtotime($value['VALIDDATE']),
        //         'birthday' => $value['BIRTHDAY'],
        //         'country' => $value['COUNTRY'],
        //         'city' => $value['CITY'],
        //         'create_at' => strtotime($value['CREATIONDATE']),
        //         'modify_time' => strtotime($value['MODIFIEDDATE']),
        //         'portrait' => $value['SEX']== 'M' ? 'default/gender1.png' : 'default/gender2.png',
        //         'status' => 1   
        //     ];
        // }

        // // dump($insertData);exit;
        // // 启动事务
        // Db::startTrans();
        // try{
        //     Db::table('d_users')->insertAll($insertData);
        //     // 提交事务
        //     Db::commit();    
        // } catch (\Exception $e) {
        //     // 回滚事务
        //     Db::rollback();
        // }

        $user = $this->user;
        $menu = self::getMenuArr();
        // dd($menu);
        
        return view('index', compact('user', 'menu'));
    }

    //无权访问跳到当前方法
    public function cuowu()
    {
        return view('error');
    }

    //上传单张图片上传
    public function uploadFile()
    {
        $file = request()->file('file');
        if ($file) {
            $info = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $pathinfo = pathinfo($info->getSaveName());
                $pic_url = $info->getSaveName();
                $res['savename'] = $pic_url;

                return success(['src'=>$pic_url], '图片上传成功');
            } else{

                return error($file->getError());
            }
        } else{
            return error('请上传图片');
        }
    }

    //上传单张图片上传到微信服务器，返回up_media_id
    public function upload_wechat_file()
    {
        $file = request()->file('file');
        if ($file) {
            $info = $file->validate(['ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $pathinfo = pathinfo($info->getSaveName());
                $pic_url = $info->getSaveName();

                $wechat = new Eswechat();
                $medias = $wechat->upload_image('uploads/'.$pic_url);
                $medias['pic_bendi'] = $pic_url;

                // dump($ss);

                return success($medias, '图片上传成功');
            } else{

                return error($file->getError());
            }
        } else{
            return error('请上传图片');
        }
    }
}
